import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Lose from '../views/lose.vue' 
import Index from '../views/index.vue'
import TakeOut from '../views/takeOut.vue'
import Repair from '../views/repair.vue'
import My from '../views/my.vue'
import Search from '../views/search.vue'
import SearchResult from '../views/searchResult.vue'
import Notice from '../views/notice.vue'
import Job from '../views/job.vue'
import NoticeDetails from '../views/noticeDetails.vue'
import JobDetails from '../views/jobDetails.vue'
import Orders from '../views/orders.vue'
import Address from '../views/address.vue'
import Setting from '../views/setting.vue'
import Edit from '../views/edit.vue'
import AddAddress from '../views/addAddress.vue'
import Shop from '../views/shop.vue'
import DishDetails from '../views/dishDetails.vue'
import Pay from '../views/pay.vue'
import PayConfirm from '../views/payConfirm.vue'
import PaySuccess from '../views/paySuccess.vue'
import OrdersDetails from '../views/ordersDetails.vue'
import Goods from '../components/goods.vue'
import Rate from '../components/rate.vue'
import Seller from '../components/seller.vue'
import Collect from '../views/collect.vue'
import AddRate from '../views/addRate.vue'
import SignUp from '../views/signUp.vue'
import MySignUp from '../views/mySignUp.vue'
 
Vue.use(VueRouter)

const white = ['/login','/lose', '/', '/takeOut', '/my', '/notice', '/noticeDetails', '/search', '/searchResult', '/job', '/jobDetails']

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/lose',
    name: 'lose',
    component: Lose
  },
  {
    path: '/',
    name: 'index',
    component: Index
  },
  {
    path: '/takeOut',
    name: 'takeOut',
    component: TakeOut
  },
  {
    path: '/repair',
    name: 'repair',
    component: Repair
  },
  {
    path: '/my',
    name: 'my',
    component: My
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  },
  {
    path: '/searchResult',
    name: 'searchResult',
    component: SearchResult
  },
  {
    path: '/job',
    name: 'job',
    component: Job
  },
  {
    path: '/notice',
    name: 'notice',
    component: Notice
  },
  {
    path: '/noticeDetails',
    name: 'noticeDetails',
    component: NoticeDetails
  },
  {
    path: '/jobDetails',
    name: 'jobDetails',
    component: JobDetails
  },
  {
    path: '/orders',
    name: 'orders',
    component: Orders
  },
  {
    path: '/address',
    name: 'address',
    component: Address
  },
  {
    path: '/setting',
    name: 'setting',
    component: Setting
  },
  {
    path: '/edit',
    name: 'edit',
    component: Edit
  },
  {
    path: '/addAddress',
    name: 'addAddress',
    component: AddAddress
  },
  {
    path: '/shop',
    name: 'shop',
    component: Shop,
    redirect: '/goods',
    children:[
      {
        path: '/goods',
        name: 'goods',
        component: Goods,
        
      },
      {
        path: '/rate',
        name: 'rate',
        component: Rate,
        
      },
      {
        path: '/seller',
        name: 'seller',
        component: Seller,
        
      },
    ],
  },
  {
    path: '/dishDetails',
    name: 'dishDetails',
    component: DishDetails
  },
  {
    path: '/pay',
    name: 'pay',
    component: Pay
  },
  {
    path: '/payConfirm',
    name: 'payConfirm',
    component: PayConfirm
  },
  {
    path: '/paySuccess',
    name: 'paySuccess',
    component: PaySuccess
  },
  {
    path: '/ordersDetails',
    name: 'ordersDetails',
    component: OrdersDetails
  },
  {
    path: '/collect',
    name: 'collect',
    component: Collect
  },
  {
    path: '/addRate',
    name: 'addRate',
    component: AddRate
  },
  {
    path: '/signUp',
    name: 'signUp',
    component: SignUp
  },
  {
    path: '/mySignUp',
    name: 'mySignUp',
    component: MySignUp
  },
  
]

const router = new VueRouter({
  mode:"history",
  routes
})

// 挂在路由导航守卫 防止用户在没有登陆的情况下直接访问主页
router.beforeEach((to, from, next) => {
  //to将要访问的路径  如果是login则直接放行
  if(white.indexOf(to.path) !== -1){
    return next()
  }

  //获取localStorage中的用户信息
  const token = window.sessionStorage.getItem('token')
  //判断用户信息是否为空  如果为空强制跳转至登陆界面  如果不为空则跳转至主页
  if(!token){
    return router.push('/login')
  }
  next()
})

export default router
